package com.shujia.mr.hanlpFenci;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class FenciDemo3 {
    public static void main(String[] args) throws Exception{
        //使用CustomDictionary
        //加载词典
        loadDictionary();

//        String info = "尚平觉得自己是数加科技29期最帅的男人，丁义杰很不服气，他说：自己才是世界上超级无敌帅的男人";

        //创建获取分词器对象
        Segment segment = HanLP.newSegment();
        BufferedReader br = new BufferedReader(new FileReader("hadoop/src/main/java/com/shujia/mr/hanlpFenci/pinglun.txt"));
        String line = null;
        while ((line=br.readLine())!=null){
            //调用方法，对一句话进行分词，默认分词
            List<Term> termList = segment.seg(line);
            for (Term term : termList) {
                System.out.println(term.word);
            }
        }
    }

    public static void loadDictionary(){
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("hadoop/src/main/java/com/shujia/mr/hanlpFenci/cidian.txt"));
            String line = null;
            while ((line=br.readLine())!=null){
                CustomDictionary.add(line);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
